Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
PEGjs | Multiple output issue

Why we can output once in PEG.js? do anyone know any other way to implement many output? I am using Stack ๐Ÿ™‚ and a function , code below:

function evalStack() {
        for (var i = stack.length - 1; 0 <= i; i--) {
            stack[i]();
        }
        return result;
    }

My custom input:

start
A=4;
A
B=8;
B

Result I expected:

4
8

Result I got:

4

Please help me

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

try this dirty solution:

all
  = _ ptp:putThenPrint+ _
  {
    var all = [];
    ptp.forEach(it => {
        all.push(it);
    });
    var r = []
    all.forEach(tp => {
      tp.toPrint.forEach(p => {
        r.push(tp.values[p])
      });
    });
    return "\n" + r.join("\n") + "\n";
  }
  
putThenPrint
  = _ mn:multiPutN _ pn:multiPrintN _ 
  {
    return {values:mn,toPrint:pn};
  }

multiPrintN
  = _ mp:printN+ _ 
  {
    var r = [];
    mp.forEach(it => {
        r.push(it);
    });
    return r;
  }

multiPutN
  = _ mp:putN+ _ 
  {
    var r = {};
    mp.forEach(it => {
        r[it[0]]=it[1];
    });
    return r;
  }
  
putN
  = _ vn:varName _ "=" _ nn:n _ ";" _ nl+ { return [vn, nn]}

printN
  = _ n:varName _ nl+ {return n;}
  
varName
  = [a-zA-Z]+ {return text();}
  
n "integer number"
  = _ [0-9]+ { return parseInt(text(), 10); }
  
nl "new line"
  = [\n]

_ "whitespace or new line"
  = [ \t]*

in the above it only give you the value of the variable in the same section so once you print the variables you can not print them again however if you change the js code inside the grammar of "all" block you can make full scan first then print all variable if this what you want, but then you will print out values before assigning. as I mentioned this is just a dirty solution that need to optimise and clean up

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!